#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <unordered_map>
#include "util.hpp"
#include <mutex>
#include"log/log.hpp"
namespace us_index
{

    struct DocInfo
    {
        std::string title_;
        std::string content_;
        std::string url_;
        int doc_id; // 文档id???????
    };

    struct InvertedPrint
    {
        //多个word对应一个docid
        int doc_id;
        int weigth;
        std::vector<std::string> words;

    };
    struct InvertedElem
    {
        int doc_id; // 文档id
        int weigth; // 权重
        std::string word;//这个word采用指针节约空间
    };

    class Index
    {
    private:
        Index() {}
        Index(const Index &) = delete;
        Index &operator=(const Index &) = delete;
        static Index *instance;

    public:
        ~Index() {}
        typedef std::vector<InvertedElem> InvertedElemList;
        static std::mutex mtx;
        static Index *GetInstance()
        {
            if (instance == nullptr)
            {
                mtx.lock();
                if (instance == nullptr)
                {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }
        // 将内容读取·出来形成前置排序
        bool BuildIndex(const std::string &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
            int count=0;
            if (!in.is_open())
            {
                std::cerr << input << " can not open" << std::endl;
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            {
                if (!BuildForwardIndex(line))
                {
                    return false;
                }
                if (!BuildInvertedIndex(forward_index.back()))
                {
                    return false;
                }
                count++;
                if(count%50==0)
                {
                    //std::cout<<"索引建立"<<count<<std::endl;

                    my_log::lg.logmessage(my_log::Level::Info,"索引建立%d",count);
                }
            }
            my_log::lg.logmessage(my_log::Level::Info,"索引建立完成");
            return true;
        }

        DocInfo *GetForwardIndex(u_int64_t doc_id)
        {
            if (doc_id >= forward_index.size())
            {
                return nullptr;
            }

            return &forward_index[doc_id];
        }

        InvertedElemList *GetInverted_Index(const std::string &keywords)
        {
            auto item = inverted_index.find(keywords);
            if (item == inverted_index.end())
            {
                std::cerr <<keywords<<" is not found in inverted_index"<<std::endl;
                return nullptr;
            }
            return &(item->second);
        }

    private:
        bool BuildForwardIndex(const std::string &line)
        {
            std::vector<std::string> results;
            results = us_util::StringUtil::CutLine(line, "\3");
            if (results.size() != 3)
            {
                std::cout << "some file docinfo was not complete " << line << std::endl;
                return false;
            }

            DocInfo doc;
            doc.title_ = results[0];
            doc.content_ = results[1];
            doc.url_ = results[2];
            doc.doc_id = forward_index.size();
            forward_index.push_back(doc);
            return true;
        }
#define X 10
#define Y 1

        bool BuildInvertedIndex(DocInfo doc)
        {
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt() : title_cnt(0), content_cnt(0)
                {
                }
            };

            std::vector<std::string> words;
            us_util::JiebaUtil::CutString(doc.title_, &words);
            std::unordered_map<std::string, word_cnt> words_map;
            for (auto &item : words)
            {
                // std::cout<<"debug: "<<item<<std::endl;
                words_map[item].title_cnt++;
            }
            us_util::JiebaUtil::CutString(doc.content_, &words);
            for (auto &item : words)
            {
                words_map[item].content_cnt++;
            }

            for (auto &item : words_map)
            {
                InvertedElem temp;
                temp.weigth = X * item.second.title_cnt + Y * item.second.content_cnt;
                temp.doc_id = doc.doc_id;
                temp.word=item.first;
                inverted_index[item.first].push_back(temp);
            }
            return true;
        }

    private:
        std::vector<DocInfo> forward_index;                               // 前序索引
        std::unordered_map<std::string, InvertedElemList> inverted_index; // 后序索引
    };

    Index *Index::instance = nullptr;
    std::mutex Index::mtx;

};